<?php

require_once 'MySQL.php';

class GrosseryList {
	
	public function __construct(){

	}
	
	/**
	 * Creates a Grossery list and links it to a user
	 * @TODO: Not working!
	 * @param int $id	-	User id
	 * @param String $name	-	Name for the list
	 */
	public function createGrosseryList($id, $name){
		$query = sprintf("	SELECT *
							FROM grossery_list
							WHERE user = %d", 
							mysql_real_escape_string($id));
		$it = MySQL::getArrayObject($query)->getIterator();
		if($it->valid()){
			return false;
		}
		$query = sprintf("INSERT INTO grossery_list(name, user) VALUES('%s', %d)",
							mysql_real_escape_string($name),
							mysql_real_escape_string($id));
		return MySQL::execute($query);
	}
	
	/**
	 * Reads an entire Grossery List
	 * Returns an Array Object containing the grossery list
	 * @param integer $id	-	User id
	 */
	public function readGrosseryList($id){
		$query = sprintf("	SELECT grossery_list_product.amount AS  'amount', 
							product.name AS  'productname', 
							product.price AS  'price', 
							product.id AS  'productid', 
							tag.name AS  'tag'
							FROM grossery_list_product
							INNER JOIN (product, grossery_list, tag)
							ON (product.id = grossery_list_product.product
							AND grossery_list.id = grossery_list_product.grossery_list
							AND product.tag = tag.id ) 
							WHERE grossery_list.id = ( 	SELECT id
														FROM  `grossery_list` 
														WHERE  `user` = %d)",		
							mysql_escape_string($id));
		return MySQL::getArrayObject($query);
	}
	
	/**
	 * Returns the id and name of a grosserylist based on the id of the user
	 */
	public function getGrosseryListInfo($id){
		$query = sprintf("	SELECT name, id
							FROM grossery_list
							WHERE user = %d", 
							mysql_real_escape_string($id));
		return MySQL::getArrayObject($query);
	}
	
	/**
	 * Returns the highest id in the grosserylist
	 */
	public function getHighestGrosseryListId(){
		$query = "SELECT MAX(user) FROM grossery_list";
		return MySQL::getFirstResult($query);
	}
	
	/**
	 * Updates the amount of a product on a grosserylist
	 * @param int $id			- The id of the list
	 * @param int $productid 	- the id of the product
	 * @param int $amount		- amount of the product to be changed in 
	 */
	public function updateAmountProduct($id, $productid, $amount){
		$query = sprintf("	UPDATE grossery_list_product 
							SET amount = %d
							WHERE grossery_list = %d
							AND product = %d",
							mysql_real_escape_string($amount),
							mysql_real_escape_string($id),
							mysql_real_escape_string($productid));
		return MySQL::execute($query);
		
	}
	
	/**
	 * Deletes an product from a list
	 * @param int $id			-	id of the list
	 * @param int $productid	-	id of the product
	 */
	public function deleteFromList($id, $productid){
		$query = sprintf("	DELETE FROM grossery_list_product
							WHERE grossery_list = %d
							AND product = %d",
							mysql_real_escape_string($id),
							mysql_real_escape_string($productid));
		return MySQL::execute($query);
	}
	
	/**
	 * Adds a product to a list
	 * @param int $id			-	id of the list
	 * @param int $productid	-	id of the product
	 * @param int $amount		-	amount of products
	 */
	public function addToList($id, $productid, $amount){
		$query = sprintf("	SELECT amount 
							FROM grossery_list_product
							WHERE grossery_list = %d
							AND product = %d",
							mysql_real_escape_string($id),
							mysql_real_escape_string($productid));
		if(MySQL::getNumRows($query) < 1){
			$query = sprintf("INSERT INTO grossery_list_product(grossery_list, product, amount)
								VALUES(%d, %d, %d)",
								mysql_real_escape_string($id),
								mysql_real_escape_string($productid),
								mysql_real_escape_string($amount));
			return MySQL::execute($query);
		}
		else{
			$amount += MySQL::getFirstResult($query);
			return $this->updateAmountProduct($id, $productid, $amount);
		}
		
	}
}